package com.scy.boot04test03.controller;

import com.scy.boot04test03.entity.Stu;
import com.scy.boot04test03.mapper.StuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StuController {
    @Autowired
    StuMapper stuMapper;

    @RequestMapping("insertStu")
    public String insert(Stu stu){
        stuMapper.insert(stu);
        return "添加成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("selectStu")
    public String select(){
        List<Stu> list = stuMapper.select();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>学生列表列表</caption>";
        html+="<tr><th>id</th><th>学生姓名</th><th>性别</th><th>年龄</th><th>住址</th><th>操作</th></tr>";
        for (Stu stu :
                list) {
            html+="<tr>";
            html+="<td>"+stu.getId()+"</td>";
            html+="<td>"+stu.getName()+"</td>";
            html+="<td>"+stu.getGender()+"</td>";
            html+="<td>"+stu.getAge()+"</td>";
            html+="<td>"+stu.getAddress()+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteStu?id="+stu.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br/>"+"<a href='/'>返回首页</a>";

    }
    @RequestMapping("deleteStu")
    public String delete(int id){
        stuMapper.delete(id);
        return "删除成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("updateStu")
    public String update(Stu stu){
        stuMapper.update(stu);
        return "更新成功<a href='/'>返回首页</a>";
    }
}
